package com.chanjiedata.changbs.module.mine;

/**
 * 文件名 : LocationSelectActivity
 * 创建者 : MangoWang
 * 创建日期 : 2019/11/27 14:56
 * 微信 : MangoWaWang
 * 邮箱 : 763482205@qq.com
 * 描述 : TODO
 */


import android.content.Intent;
import android.graphics.Color;
import android.os.Build;

import androidx.annotation.RequiresApi;

import android.os.Bundle;

import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.chanjiedata.changbs.R;
import com.chanjiedata.changbs.base.BaseActivity;
import com.chanjiedata.changbs.net.Apis;
import com.chanjiedata.changbs.net.NetworkManager;
import com.chanjiedata.changbs.net.interfaces.OnRequestCallBack;
import com.chanjiedata.changbs.proto.Area;
import com.chanjiedata.changbs.utlis.StatusBarUtil;
import com.chanjiedata.changbs.utlis.ValidateUtils;
import com.chanjiedata.changbs.view.easyadapter.BaseViewHolder;
import com.chanjiedata.changbs.view.easyadapter.RecyclerArrayAdapter;
import com.google.protobuf.InvalidProtocolBufferException;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class LocationSelectActivity extends BaseActivity {

    @BindView(R.id.title_tv)
    TextView titleTv;
    @BindView(R.id.location_rv)
    RecyclerView locationRv;

    private RecyclerArrayAdapter<Area.area> mAdapter;
    private String areaId = "0";
    private int page = 0;
    private List<Area.area> dataList = new ArrayList<>();  // 请求中数据
    private String province;

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_location_select);
        StatusBarUtil.setStatusBarLightMode(this, Color.parseColor("#ffffff"), true);
        ButterKnife.bind(this);
        titleTv.setText("展区");
        initRecycle();
        loadData();
    }

    private void loadData() {
        // 加载底部数据
        showProgress();

        Area.area_list.Builder builder = Area.area_list.newBuilder();
        builder.setAreaId(areaId);
        NetworkManager.INSTANCE.post(Apis.get_areas_list, builder.build().toByteArray(), new OnRequestCallBack() {
            @Override
            public void onOk(byte[] response) throws InvalidProtocolBufferException {
                hideProgress();
                if (titleTv == null) return; // 防止请求延迟出现闪退
                page++;


                List<Area.area> list = Area.area_list.parseFrom(response).getAreaListList();

                if (!ValidateUtils.isValidate(list)) {
                    showToast("没有更多了");
                    page--;
                    return;
                }
                dataList.clear();
                mAdapter.clear();
                dataList.addAll(list);
                mAdapter.addAll(list);
            }

            @Override
            public void onError(int errorCode, String errorMessage) {
                hideProgress();
                Toast.makeText(LocationSelectActivity.this, errorMessage, Toast.LENGTH_SHORT).show();
            }

        });

    }

    /**
     * y
     * 实例化底部RecyclerView
     * *
     */
    private void initRecycle() {
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        locationRv.setLayoutManager(layoutManager);
        locationRv.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
        locationRv.setAdapter(mAdapter = new RecyclerArrayAdapter<Area.area>(mContext) {
            @Override
            public BaseViewHolder OnCreateViewHolder(ViewGroup parent, int viewType) {
                return new ViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item_location, parent, false));
            }

            class ViewHolder extends BaseViewHolder<Area.area> {
                TextView location;

                public ViewHolder(View itemView) {
                    super(itemView);
                    location = itemView.findViewById(R.id.tv_location);
                }

                @Override
                public void setData(Area.area data) {
                    location.setText(data.getAreaName());
                }
            }
        });
        mAdapter.setOnItemClickListener(position -> {
            Area.area locationBean = dataList.get(position);
            areaId = locationBean.getAreaId();
            if (page == 2) {
                Intent intent = new Intent();
                intent.putExtra("areaId", areaId);
                intent.putExtra("area_name", locationBean.getAreaName());
                intent.putExtra("area_province", province);
                setResult(RESULT_OK, intent);
                finish();
            } else {
                province = locationBean.getAreaName();
                loadData();
            }

        });
    }

    @OnClick(R.id.back_iv)
    public void onViewClicked() {
        finish();
    }
}
